package com.regal.reg;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.spark.util.DummySSLSocketFactory;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

public class RegControler {

	private String userType;
	private String userName;
	private String passwd;
	private String nickName;
	private String realName;
	private String sex;
	private String position;
	private String mobile;
	private String email;
	private String companyName;
	private String companyid;
	private String phoneNo;
	private String fax;
	private String address;
	private String tradename;
	private String tradename2;
	private String tradename3;
	private String trade1;
	private String trade2;
	private String trade3;
	private String server;

	private static RegControler singleton;

	private static final Object LOCK = new Object();

	private RegControler() {
		
	}

	public static RegControler getInstance() {
		// Synchronize on LOCK to ensure that we don't end up creating
		// two singletons.
		synchronized (LOCK) {
			if (null == singleton) {
				RegControler controller = new RegControler();
				singleton = controller;
				return controller;
			}
		}
		return singleton;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCompanyid() {
		return companyid;
	}

	public void setCompanyid(String companyid) {
		this.companyid = companyid;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getTrade1() {
		return trade1;
	}

	public void setTrade1(String trade1) {
		this.trade1 = trade1;
	}

	public String getTrade2() {
		return trade2;
	}

	public void setTrade2(String trade2) {
		this.trade2 = trade2;
	}

	public String getTrade3() {
		return trade3;
	}

	public void setTrade3(String trade3) {
		this.trade3 = trade3;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getTradename() {
		return tradename;
	}

	public void setTradename(String tradename) {
		this.tradename = tradename;
	}

	public String getTradename2() {
		return tradename2;
	}

	public void setTradename2(String tradename2) {
		this.tradename2 = tradename2;
	}

	public String getTradename3() {
		return tradename3;
	}

	public void setTradename3(String tradename3) {
		this.tradename3 = tradename3;
	}
	
	
	public XMPPConnection getConnection() throws XMPPException {
		final LocalPreferences localPreferences = SettingsManager
				.getLocalPreferences();
		XMPPConnection connection = null;

		// Get connection

		int port = localPreferences.getXmppPort();

		int checkForPort = server.indexOf(":");
		if (checkForPort != -1) {
			String portString = server.substring(checkForPort + 1);
			if (ModelUtil.hasLength(portString)) {
				// Set new port.
				port = Integer.valueOf(portString);
			}
		}

		boolean useSSL = localPreferences.isSSL();
		boolean hostPortConfigured = localPreferences.isHostAndPortConfigured();

		ConnectionConfiguration config = null;

		if (useSSL) {
			if (!hostPortConfigured) {
				config = new ConnectionConfiguration(server, 5223);
				config.setSocketFactory(new DummySSLSocketFactory());
			} else {
				config = new ConnectionConfiguration(localPreferences
						.getXmppHost(), port, server);
				config.setSocketFactory(new DummySSLSocketFactory());
			}
		} else {
			if (!hostPortConfigured) {
				config = new ConnectionConfiguration(server);
			} else {
				config = new ConnectionConfiguration(localPreferences
						.getXmppHost(), port, server);
			}

		}

		if (config != null) {
			config.setReconnectionAllowed(true);
			boolean compressionEnabled = localPreferences
					.isCompressionEnabled();
			config.setCompressionEnabled(compressionEnabled);
			connection = new XMPPConnection(config);
		}

		if (connection != null) {
			connection.connect();
		}
		return connection;

	}
	

}
